// pages/venue/Venue/index.js
const app = getApp();
const ajax = app.myRequest()
var util = require('../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hot: [

        ],
        getId: 0,
        option1: [
            { text: '附近', value: 0 }
        ],
        option2: [
            { text: '健身馆', value: 1 },
            { text: '瑜伽馆', value: 2 },
            { text: '游泳馆', value: 3 },
            { text: '拳击馆', value: 4 },
            { text: '普拉提馆', value: 5 },
            { text: '私教工作室', value: 6 },
        ],
        option3: [
            { text: '默认排序', value: 0 },
            { text: '好评排序', value: 1 },
            { text: '销量排序', value: 2 },
        ],
        value1: 0,
        typeOne: 0,
        type: 1,
        time: 1999999
    },


    onLoad: function (options) {
        // 获取经纬度
        var _this = this
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                _this.setData({ latitude: res.latitude, longitude: res.longitude })
                _this.getAddress()
                _this.getVenue()
            }
        })
    },

    //   获取定位信息 并转换成语义化信息
    getAddress() {
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        var _this = this
        wx.request({
            url: 'https://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + latitude + ',' + longitude + "&key=" + '5ASBZ-RDYC6-UCWS2-ENPLN-7ROKF-OVFNA' + "&get_poi=1",
            success: res => {
                _this.setData({ address: res.data.result.address_component.city });
            }
        })
    },

    getNumber(e) {
        this.setData({ getId: e.currentTarget.dataset.id })
    },

    //  分类 场馆 
    onSwitch1Change(detail) {
        var type = detail.detail
        this.setData({ type })
        this.getClassification()
    },

    // 获取 分类 场馆
    getClassification() {
        var _this = this
        var type = _this.data.type
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        ajax.getRequest('/Gymnasium/index', { latitude, longitude, type }).then((res) => {
            _this.setData({ hot: res.data.data.data })
        })
    },

    // 点击获取位置
    getMap() {
        var _this = this
        wx.chooseLocation({
            success: function (res) {
                _this.setData({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    address: res.address
                })
                _this.getClassification()
            }
        })
    },

    // 搜索 场馆名
    userName(e) {
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        var name = e.detail.value
        this.setData({ name })
        var _this = this
        ajax.getRequest('/Gymnasium/index', { latitude, longitude, name }).then((res) => {
            _this.setData({ hot: res.data.data.data })
        })

    },

    // 获取 附近场馆
    getVenue() {
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        var _this = this
        ajax.getRequest('/Gymnasium/index', { latitude, longitude, }).then((res) => {
            wx.hideLoading();
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
            _this.setData({
                hot: res.data.data.data,
                current_page: res.data.data.current_page,
                last_page: res.data.data.last_page,
                per_page: res.data.data.per_page
            })
        })
    },

    // 跳转到场馆主页
    goVenue(e) {
        var index = e.currentTarget.dataset.id
        var latitude = this.data.hot[index].latitude
        var longitude = this.data.hot[index].longitude
        var id = this.data.hot[index].id
        wx.navigateTo({ url: '../venueHome/index?latitude=' + latitude + '&longitude=' + longitude + '&id=' + id })
    },

    // 下拉刷新
    onPullDownRefresh: function () {
        var type = this.data.type
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        var _this = this
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        ajax.getRequest('/Gymnasium/index', { latitude, longitude, type }).then((res) => {
            _this.setData({ hot: res.data.data.data, user: '' })
        })
    },

    // 上拉加载
    onReachBottom: function () {
        var _this = this
        var longitude = this.data.longitude
        var latitude = this.data.latitude
        var type = this.data.type
        var current_page = this.data.current_page

        wx.showLoading()
        setTimeout(function () { wx.hideLoading() }, 3000)

        if (type == 0) {
            ajax.getRequest('/Gymnasium/index', { longitude, latitude, page: current_page + 1 }).then((res) => {
                var hot = _this.data.hot.concat(res.data.data.data)
                _this.setData({
                    hot: hot,
                    current_page: res.data.data.current_page,
                    total: res.data.data.total
                })
            })
        } else {
            ajax.getRequest('/Gymnasium/index', { longitude, latitude, type, page: current_page + 1 }).then((res) => {
                var hot = _this.data.hot.concat(res.data.data.data)
                _this.setData({
                    hot: hot,
                    current_page: res.data.data.current_page,
                    total: res.data.data.total
                })
            })
        }
    },
})